<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\store\controller;

use app\store\controller\Controller;
use app\store\model\RecommendContent as RecommendModel;
use app\store\model\RecommendImage as RecommendImageModel;
use app\store\model\RecommendGoods as RecommendGoodsModel;
use app\common\model\Goods as GoodsModel;
/**
 * 文章管理控制器
 * Class article
 * @package app\store\controller\content
 */
class Recommend extends Controller
{
    /**
     * 文章列表
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function list()
    {
        $model = new RecommendModel;
        $list = $model->getList($this->request->param());
        // var_dump($list);
        return $this->renderSuccess(compact('list'));
    }

     /**
     * 获取商品列表
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function Goodslist()
    {
       // 获取列表记录
       $model = new GoodsModel;
       $list = $model->getList($this->request->param());
       $newlist = array();
       foreach($list as $k=>$v){
        $newlist[$k]['goods_id'] = $v['goods_id'];
        $newlist[$k]['goods_name'] = $v['goods_name'];
       }
       return $this->renderSuccess(compact('newlist'));
    }

    /**
     * 文章详情
     * @param int $recommendId
     * @return array|bool|string
     */
    public function detail(int $recommendId)
    {
        $detail = RecommendModel::detail($recommendId);
        $RecommendImageModel = new RecommendImageModel();
        $RecommendGoodsModel = new RecommendGoodsModel();
        // 遍历商品列表整理数据
        $detail['carousel_images'] =  $RecommendImageModel->getDataList((int)$detail['recommend_id']);
        $detail['goods_ids'] =  $RecommendGoodsModel->getRecommendGoodsIds((int)$detail['recommend_id']);
        // $detail['describe'] =   strip_tags(htmlspecialchars_decode($detail['describe']));        
        $detail['describe'] =   htmlspecialchars_decode($detail['describe']);        

        // var_dump($detail);
        // 获取image (这里不能用with因为编辑页需要image对象)
        !empty($detail) && $detail['image'];
        return $this->renderSuccess(compact('detail'));
    }

    /**
     * 添加文章
     * @return array|mixed
     */
    public function add()
    {
        // 新增记录
        $model = new RecommendModel;
        if ($model->add($this->postForm())) {

            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 更新文章
     * @param int $articleId
     * @return array|mixed
     */
    public function edit(int $recommendId)
    {
        // 文章详情
        $model = RecommendModel::detail($recommendId);
        // 更新记录
        if ($model->edit($this->postForm())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除文章
     * @param $articleId
     * @return array
     */
    public function delete(int $recommendId)
    {
        // 文章详情
        $model = RecommendModel::detail($recommendId);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

}
